<?php


namespace api\home\controller;


use api\home\model\OrderModel;
use cmf\controller\RestBaseController;
use think\facade\Db;
use think\Request;


class OrderController extends RestBaseController
{
    public function createOrder(Request $request)
    {
        $data = input('post.');
        //查询

        $goods = Db::name('goods')
            ->where('id',$data['goods_id'])
            ->find();

//        $res = $this->getPirce($data['goods_id'],$data['goods_info']);
        $price = $data['goods_info']['价格'];

        if(!$goods){
            $this->error('商品不存在');
        }
        if($goods['status'] != 1){
            $this->error('商品缺货或已下架');
        }

        //收货地址
        $address = Db::name('address')
            ->where('id',$data['address_id'])
            ->find();
        if(!$address){
            $this->error('收货地址不存在');
        }
        $total_no = date('YmdHis').mt_rand(1111,9999).createRandStr(4);
        $insert_data = [
            'user_id' => $request->userId,
            'goods_name' => $goods['name'],
            'goods_info' => json_encode( $data['goods_info']),
            'price' => $price,
            'num' => $data['num'],
            'total_price' => round($price*$data['num'],2),
            'trade_no' => $total_no,
            'user_name' => $address['name'],
            'user_tel' => $address['mobile'],
            'user_address' => $address['address'].$address['address_more'],
            'status' => 0,
            'remark' => $data['remarks'],
            'create_time' => date('Y-m-d H:i:s')

        ];

        $model = new OrderModel();
        $create = $model->save($insert_data);
        if($create){
            $this->success('订单创建成功',[
                'payInfo' => [
                    'order_id' => $model->id,
                    'trade_no'=>$total_no
                ]
            ]);
        }else{
            $this->error('订单创建失败');
        }

    }


    private function getPirce($goods_id,$goods_info)
    {

        $goods = Db::name('pay')
            ->where('id','=',$goods_id)
            ->find();

        $info = json_encode($goods['goods_info'],true);
        foreach ($info as $value){
            if($value['尺码']==$goods_info['尺码'] && $value['颜色']==$goods_info['颜色']){
                return $value;
            }
        }
    }

    //查询商品详情
    public function getGoodsInfo()
    {
        $goods_id = input('goods_id/d');
        $goods = Db::name('goods')
            ->find($goods_id);
        unset($goods['goods_info']);
        $goodsAttr = json_decode($goods['attr'],true);
        foreach ($goodsAttr as $key => $val)
        {
            $goodsAttr[$key]['attr_value'] = explode(',',$val['attr_value']);
        }
        $goods['attr'] = $goodsAttr;
        $this->success('商品详情',$goods);
    }

    //方法2
    public function getGoodsInfo1($goods,$buy)
    {
        $goodsSku = json_decode($goods['goods_info'],true);
        //循环
        foreach ($goodsSku as $sku){
            $hasSku = true;

            foreach ($buy as $attr => $val){
                if ($sku[$attr] != $val){
                    $hasSku = false;
                }
            }
            if ($hasSku){
                return $sku;
            }else{
                continue;
            }
        }
    }



















}